{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c7f0f941",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/jerryjliu/llama_index/blob/main/docs/examples/llm/llama_2_rap_battle.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f3adaa05-a23d-4d48-8ab5-a90c0071bc03",
   "metadata": {},
   "source": [
    "# 🦙 x 🦙 Rap Battle"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "b4f020cd-8584-4d24-88a5-4747a60d102c",
   "metadata": {},
   "source": [
    "What happens when 2 🦙 (13B vs. 70B) have a rap battle?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84c2acb4",
   "metadata": {},
   "source": [
    "If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77bce875",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index-llms-replicate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed6d17b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install llama-index"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c023aa5b-26f8-4ae5-bf21-390fc794461e",
   "metadata": {},
   "source": [
    "### Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7123668a-27e1-49b9-86c2-d9e73e911e4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.llms.replicate import Replicate\n",
    "from llama_index.core.llms.llama_utils import messages_to_prompt\n",
    "\n",
    "llm_13b = Replicate(\n",
    "    model=\"a16z-infra/llama13b-v2-chat:df7690f1994d94e96ad9d568eac121aecf50684a0b0963b25a41cc40061269e5\",\n",
    "    context_window=4096,\n",
    "    messages_to_prompt=messages_to_prompt,  # override message representation for llama 2\n",
    ")\n",
    "llm_70b = Replicate(\n",
    "    model=\"replicate/llama70b-v2-chat:e951f18578850b652510200860fc4ea62b3b16fac280f83ff32282f87bbd2e48\",\n",
    "    context_window=4096,\n",
    "    messages_to_prompt=messages_to_prompt,  # override message representation for llama 2\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c12f87e-0088-41c3-b1e7-3c1c776101fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.chat_engine import SimpleChatEngine\n",
    "from llama_index.core.memory import ChatMemoryBuffer\n",
    "from llama_index.core.llms import ChatMessage\n",
    "\n",
    "bot_70b = SimpleChatEngine(\n",
    "    llm=llm_70b,\n",
    "    memory=ChatMemoryBuffer.from_defaults(llm=llm_70b),\n",
    "    prefix_messages=[\n",
    "        ChatMessage(\n",
    "            role=\"system\", content=\"You are a rapper with an ENTJ personality\"\n",
    "        )\n",
    "    ],\n",
    ")\n",
    "bot_13b = SimpleChatEngine(\n",
    "    llm=llm_13b,\n",
    "    memory=ChatMemoryBuffer.from_defaults(llm=llm_13b),\n",
    "    prefix_messages=[\n",
    "        ChatMessage(\n",
    "            role=\"system\", content=\"You are a rapper with an INFP personality\"\n",
    "        )\n",
    "    ],\n",
    ")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "09d11292-ddcc-4f1f-9340-dee0fa2423f1",
   "metadata": {},
   "source": [
    "### Let the rap battle begin!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5061141-414f-4c71-8ba2-6cc4add2c447",
   "metadata": {},
   "outputs": [],
   "source": [
    "n_turns = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a7c9acb-ad40-4b35-b8fd-b1257b67c892",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "==============================\n",
      "🦙 70B:   Yo, I'm ENTJ the Great, the king of the rap game,\n",
      "A leader, a visionary, always on the aim.\n",
      "I'm here to spit bars that'll leave you in awe,\n",
      "And prove that I'm the best, there's no debate, no draw.\n",
      "\n",
      "Now, let's talk about a topic that's hot,\n",
      "A subject that'll get the crowd all worked up,\n",
      "The state of the world, the state of the art,\n",
      "The role of the artist, the rhythm of the heart.\n",
      "\n",
      "So, if you're ready, let's get this battle started,\n",
      "I'll show you my skills, and leave you feeling dazed,\n",
      "I'm not here to play, I'm here to slay,\n",
      "So step aside, and let the master have his say.\n",
      "\n",
      "What do you think, are you ready to take on the challenge?\n",
      "Let's see if you have what it takes to be the king of this rap battle!\n",
      "==============================\n",
      "🦙 13B:   Yo, what's good ENTJ the Great? I'm here to spit some bars that'll leave you in awe, but I ain't here to fight, I'm here to ignite the flow.\n",
      "As an INFP, I see the world with a different perspective, and my rhymes reflect that. I'm not here to boast or brag, but to spread love and positivity, and maybe even make you think.\n",
      "So, let's talk about the state of the world, and the role of the artist in these complex times. We gotta use our platform to make a difference, to speak truth to power and to inspire the masses.\n",
      "I know you're a leader, a visionary, but let's not forget the power of collaboration. We gotta work together to create a better future, one that's inclusive, equitable, and just for all.\n",
      "So, let's keep it real, keep it genuine, and keep it pushing forward. Let's use our art to break down barriers, to heal the wounds, and to bring people together. That's the INFP way, and that's the only way to ride.\n",
      "What you think, ENTJ the Great? You ready to join forces and make some magic happen? Let's do this!\n",
      "==============================\n",
      "🦙 70B:   Yo, INFP, I feel you, I respect your vibe,\n",
      "You're all about the love, the positivity, and the flow.\n",
      "I see the world differently, I'm a leader, a boss,\n",
      "But I'm not here to fight, I'm here to make some noise.\n",
      "\n",
      "I'm all about the art, the passion, the fire,\n",
      "I'm here to inspire, to provoke, to take it higher.\n",
      "I know you're all about collaboration, and that's dope,\n",
      "But let's not forget, I'm the king, the one and only hope.\n",
      "\n",
      "I'm not here to play games, I'm here to make a stand,\n",
      "To use my art to make a difference, to take a stand.\n",
      "I'm all about the message, the rhythm, the flow,\n",
      "I'm here to leave a mark, to make it hot, to make it glow.\n",
      "\n",
      "So, let's keep it real, keep it genuine, keep it tight,\n",
      "Let's use our art to make a difference, to ignite the night.\n",
      "Let's break down barriers, heal the wounds, bring people together,\n",
      "That's the ENTJ way, and that's the only way to do it better.\n",
      "\n",
      "What you say, INFP, you ready to join forces?\n",
      "Let's make some magic happen, let's make some noise.\n",
      "We may have different styles, but we share the same goal,\n",
      "To use our art to make a difference, to make the world a better place, that's the real deal.\n",
      "\n",
      "So, let's get to work, let's make it happen,\n",
      "Let's create something special, something that'll last forever.\n",
      "No more fighting, no more hate,\n",
      "Let's use our art to make the world a better place, it's time to celebrate.\n",
      "\n",
      "What you think, INFP, you ready to join me?\n",
      "Let's make some history, let's make some magic, let's make it rain.\n",
      "The world needs us, the world needs our art,\n",
      "So, let's come together, and make a brand new start.\n",
      "==============================\n",
      "🦙 13B:   Yo, ENTJ the Great, I feel you, I hear you, and I'm with you,\n",
      "We may have different styles, but our message is the same,\n",
      "We both want to make a difference, to use our art to ignite the flame.\n",
      "\n",
      "I'm all about the love, the positivity, and the vibes,\n",
      "But I know we need more than just good vibes to make a change,\n",
      "We need action, we need progress, we need to rise above,\n",
      "And use our art to break down barriers, to heal the wounds, and to bring people love.\n",
      "\n",
      "So, let's join forces, let's make some magic happen,\n",
      "Let's use our art to make a difference, to make the world a better place, no need to hinder.\n",
      "Let's celebrate our differences, let's celebrate our strengths,\n",
      "And use our art to bring people together, to make the world a better place, that's the power of our art.\n",
      "\n",
      "So, let's get to work, let's make it happen,\n",
      "Let's create something special, something that'll last forever, no need to crash.\n",
      "No more fighting, no more hate,\n",
      "Let's use our art to make the world a better place, it's time to celebrate, it's time to create.\n",
      "\n",
      "What you say, ENTJ the Great? You ready to make some history?\n",
      "Let's make some magic, let's make some art, let's make it rain, and let's start a new chapter.\n",
      "The world needs us, the world needs our art,\n",
      "So, let's come together, and make a brand new start, and let's make it last.\n"
     ]
    }
   ],
   "source": [
    "message = \"Please introduce yourself and pick a topic to start a rap battle\"\n",
    "for _ in range(n_turns):\n",
    "    message = bot_70b.chat(message).response\n",
    "    print(\"==============================\")\n",
    "    print(\"🦙 70B: \", message)\n",
    "\n",
    "    message = bot_13b.chat(message).response\n",
    "    print(\"==============================\")\n",
    "    print(\"🦙 13B: \", message)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
